#pragma once
#include<string>
#include<ctemplate/template.h>
#include<vector>
#include"oj_model.hpp"

namespace ns_view
{
    using namespace ns_model;
    class View
    {
        private:
        std::string AllQuestionPath="./template_html/all_questions.html";
        std::string OneQuestionPath="./template_html/one_question.html";
        public:
        View()
        {
        }
        ~View()
        {      
        }
       bool AllExpendHtml(std::vector<struct Question> &v1,std::string *html)
       {
            //形成字典
            ctemplate::TemplateDictionary root("all_questions");
            for(auto &q:v1)
            {
                ctemplate::TemplateDictionary *sub=root.AddSectionDictionary("question_list");
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);

            }

            //获取被绚烂的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(AllQuestionPath, ctemplate::DO_NOT_STRIP);

            //完成渲染
            tpl->Expand(html, &root);

            return true;
       }
       bool OneExpendHtml(struct Question q,std::string *html)
       {

        ctemplate::TemplateDictionary root("one_question");
        root.SetValue("number", q.number);
        root.SetValue("title", q.title);
        root.SetValue("star", q.star);
        root.SetValue("desc", q.desc);
        root.SetValue("pre_code", q.header);

        //获取被渲染的html
        ctemplate::Template *tpl = ctemplate::Template::GetTemplate(OneQuestionPath, ctemplate::DO_NOT_STRIP);
       
        //完成渲染功能
        tpl->Expand(html, &root);
            return true;
       }
    };
}